<?php

/**

 * 发起工作流程
 * 
 * @access  public 
 * @author  zhangyingheng<zhang_yh@jsumt.com.cn> 
 */

namespace Index\Controller;

//use Think\Controller;

class IndexController extends BaseController {

    public function _initialize() {
        parent::_initialize();
        $this->title = '流程审批';
        $this->webid = M('web')->where('webname="流程审批"')->getField('id');
    }

    //后台首页
    public function index() {
        //获取流程未办结条数
        $map['USER_ID'] = session('user_id');
        //$map['USER_ID'] = 'yanbo';
        $map['PRCS_FLAG'] = array('in', [1, 2]);
        $ids = M('flow_run_prcs')->field('run_id,user_id')->where($map)->getField('run_id', true);
        if (!is_array($ids)) {
            $ids[] = $ids;
        }
        $rMap['RUN_ID'] = array('in', $ids);
        $rMap['DEL_FLAG'] = 0;
        $count = M('flow_run')->where($rMap)->count();
        $this->flowCount = $count;

        //流程已办结条数
        $map['PRCS_FLAG'] = array('in', [3, 4]);
        $ids = M('flow_run_prcs')->field('run_id,user_id')->where($map)->getField('run_id', true);
        if (!is_array($ids)) {
            $ids[] = $ids;
        }
        $rMap['RUN_ID'] = array('in', $ids);
        $rMap['DEL_FLAG'] = 0;
        $count = M('flow_run')->where($rMap)->count();
        $this->finishCount = $count;

        //获取常用流程 
        unset($map);
        $map['is_show'] = 1;
        $data = M('flow_type')->field('FLOW_ID,FLOW_NAME,FORM_ID,FLOW_TYPE,FLOW_NO,is_show,icon')->where($map)->order('flow_no')->select();
        $this->uses = $data;

        $this->display();
    }

    /* 接收消息
     * 调试不成功，微信回调函数默认端口80
     * wx.jsumt.com.cn => 通过新浪云中转，会自动过滤回传的url的参数 
     * www.jsumt.com.cn:88 => 不能自定义端口
     */

    public function message() {
        vendor('qywx.WXBizMsgCrypt');
        $web = M("web")->where('id=' . $this->webid)->find();
        $token = $web['token'];
        $encodingAesKey = $web['aeskey'];
        $corpId = $web['wx_corpid'];

        $sVerifyMsgSig = urlencode(I('msg_signature'));
        $sVerifyTimeStamp = urlencode(I('timestamp'));
        $sVerifyNonce = urlencode(I('nonce'));
        $sVerifyEchoStr = urlencode(I('echostr'));

        // 需要返回的明文
        $EchoStr = "";

        $wxcpt = new \WXBizMsgCrypt($token, $encodingAesKey, $corpId);
        $errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
        if ($errCode == 0) {
            //echo $errCode;
            // 验证URL成功，将sEchoStr返回
            // HttpUtils.SetResponce($sEchoStr);
        } else {
            print("ERR: " . $errCode . "\n\n");
        }
    }

    public function flowCate() {
        //通过部门\角色\用户获取相关流程
        //获取所有流程的第一步
        $firstSteps = M('flow_process')->field('FLOW_ID,PRCS_ID,PRCS_DEPT,PRCS_PRIV,PRCS_USER')->where('PRCS_ID=1')->select();
        $newData = array();
        foreach ($firstSteps as $key => $row) {
            if (($row['prcs_dept'] == 'ALL_DEPT' || $row['prcs_dept'] == '') && ($row['prcs_priv'] == '') && ($row['prcs_user'] == '') || (strpos($row['prcs_dept'], session('dept_id')) !== false) || (strpos($row['prcs_priv'], session('user_priv')) !== false) || (strpos($row['prcs_user'], session('user_id')) !== false)) {
                $newData[] = $row;
                $flow_ids[] = $row['flow_id'];
            }
        }
        $map['FLOW_ID'] = array('in', $flow_ids);
        $map['icon'] = array('neq', '');
        $data = M('flow_type')->field('flow_id,flow_name,form_id,flow_sort,is_show,icon,color')->where($map)->select();
        foreach ($data as $key => $row) {
            if (strpos($row['flow_name'], '固定资产') !== false) {
                $row['flow_name'] = msubstr($row['flow_name'], 4, 5, "UTF-8", false);
            }
            if (strpos($row['flow_name'], '低值易耗品') !== false) {
                $row['flow_name'] = msubstr($row['flow_name'], 5, 5, "UTF-8", false);
            }
            if (strpos($row['flow_name'], '工作令号') !== false) {
                $row['flow_name'] = msubstr($row['flow_name'], 4, 5, "UTF-8", false);
            }
            $flows[$row['flow_sort']][] = $row;
        }
        foreach ($flows as $key => $row) {
            $sorts[$key]['sortName'] = M('flow_sort')->where('SORT_ID=' . $key)->getField('sort_name');
            $sorts[$key]['lists'] = $row;
        }

//        $data = M('flow_sort')->order('sort_no')->where('sort_no != 0')->select();
//        foreach ($data as $key => $row) {
//            $sorts[$key]['sortName'] = $row['sort_name'];
//            $flows = M('flow_type')->field('flow_id,flow_name,form_id,flow_sort,is_show,icon,color')->where('flow_sort=' . $row['sort_id'])->select();
//            foreach ($flows as $k_flow => $v_flow) {
//                if (strpos($v_flow['flow_name'], '固定资产') !== false) {
//                    $flows[$k_flow]['flow_name'] = msubstr($v_flow['flow_name'], 4, 5, "UTF-8", false);
//                }
//                if (strpos($v_flow['flow_name'], '低值易耗品') !== false) {
//                    $flows[$k_flow]['flow_name'] = msubstr($v_flow['flow_name'], 5, 5, "UTF-8", false);
//                }
//            }
//            $sorts[$key]['lists'] = $flows;
//        }
        // print_r($sorts);
        $this->sorts = $sorts;
        $this->display();
    }

    public function childFlow() {
        $flow_sort = I('id');
        $this->childs = M('flow_type')->field('flow_id,flow_name,form_id,flow_sort')->where('flow_sort=' . $flow_sort)->select();
        $this->display();
    }

    public function form() {
        if (IS_AJAX) {
            $data = I('post.');
            $type = I('get.type');

            //如果run_id存在，修改表单
            if (I('RUN_ID')) {
                unset($data['RUN_ID']);
                $flow_id = M('flow_run')->where('RUN_ID=' . I('RUN_ID'))->getField('flow_id');
                $table = 'flow_data_' . $flow_id;
                foreach ($data as $key => $row) {
                    if (substr($key, 0, 4) == 'DATA') {
                        $saveData[strtolower($key)] = $row;
                    }
                }
                $saveRes = M($table)->where('run_id=' . I('RUN_ID'))->save($saveData);

                $flag = true;
                if ($data['feedback'] == '') {
                    if ($data['PRCS_ID'] == 1) {
                        $flag = true;
                    } else {
                        $map['RUN_ID'] = I('RUN_ID');
                        $map['PRCS_ID'] = $data['PRCS_ID'];
                        $map['USER_ID'] = session('user_id');
                        $count = M('flow_run_feedback')->where($map)->count();
                        if (!$count) {
                            $flag = false;
                        }
                        //判断当前用户会签条件
                        unset($map);
                        $map['FLOW_ID'] = $data['FLOW_ID'];
                        $map['PRCS_ID'] = $data['PRCS_ID'];
                        $feedback = M('flow_process')->where($map)->getField('feedback');
                        if ($feedback == 2) {
                            $flag = true;
                        }
                    }
                } else {
                    //防止重复提交
                    unset($map);
                    $map['RUN_ID'] = I('RUN_ID');
                    $map['PRCS_ID'] = $data['PRCS_ID'];
                    $map['USER_ID'] = session('user_id');
                    $feedCount = M('flow_run_feedback')->where($map)->count();
                    if (!$feedCount) {
                        //添加会签意见
                        $fData['RUN_ID'] = I('RUN_ID');
                        $fData['PRCS_ID'] = $data['PRCS_ID'];
                        $fData['FLOW_PRCS'] = $data['PRCS_ID']; //升级后增加的字段，不知道有什么用，暂时和prcs_id一样
                        $fData['USER_ID'] = session('user_id');
                        $fData['CONTENT'] = $data['feedback'];
                        $fData['EDIT_TIME'] = date('Y-m-d H:i:s');
                        $res = M('flow_run_feedback')->add($fData);
                    }
                }
                // var_dump($flag);die;
                if ($flag) {
                    if ($saveRes !== false || $res !== false) {
                        if ($type == 1) {
                            $return['info'] = '修改成功';
                            $return['url'] = U('Flow/lists');
                        } else if ($type == 2) {
                            //取当前用户步骤信息
                            unset($map);
                            $map['RUN_ID'] = I('RUN_ID');
                            $map['FLOW_PRCS'] = $data['PRCS_ID'];
                            $map['USER_ID'] = session('user_id');
                            $row = M('flow_run_prcs')->where($map)->find();

                            //判断是否为无主办人会签
                            unset($map);
                            $map['FLOW_ID'] = $data['FLOW_ID'];
                            $map['PRCS_ID'] = $data['PRCS_ID'];
                            $default = M('flow_process')->where($map)->getField('TOP_DEFAULT');

                            //如果是无主办人会签的话，改当前用户状态为4,如果是有主办人会签，改状态为2，加完成时间
                            $smap['RUN_ID'] = I('RUN_ID');
                            $smap['FLOW_PRCS'] = $data['PRCS_ID'];
                            $smap['USER_ID'] = session('user_id');
                            if ($row['op_flag']) {
                                $sData['PRCS_FLAG'] = 2;    //主办人点击办理完毕时不改状态
                            } else {
                                $sData['PRCS_FLAG'] = 4;    //经办人点击办理完毕时状态改为4
                            }

                            $sData['DELIVER_TIME'] = date('Y-m-d H:i:s');
                            M('flow_run_prcs')->where($smap)->save($sData);

                            //判断是否所有人都会签完毕，如果有主办人会签的话，不包含主办人
                            unset($map);
                            $map['RUN_ID'] = I('RUN_ID');
                            $map['FLOW_PRCS'] = $data['PRCS_ID'];
                            $map['OP_FLAG'] = 0;    //查询所有从办人
                            $finish = M('flow_run_prcs')->field('RUN_ID,FLOW_PRCS,OP_FLAG,DELIVER_TIME,PRCS_FLAG')->where($map)->select();
                            $count = 0;
                            foreach ($finish as $f_key => $f_row) {
                                //标记为4
                                if ($f_row['prcs_flag'] != 4) {
                                    $count ++;
                                }
                            }
                            if ($default != 2) {
                                //有主办人会签，由主办人转交下一步
                                if ($row['op_flag'] && $count == 0) {
                                    $count = 0;
                                } else {
                                    $count = 1;
                                }
                            }
                            $return['info'] = '办理结束';
                            if (!$count) {
                                $return['url'] = U('nextStep', array('run_id' => I('RUN_ID'), 'flow_id' => $data['FLOW_ID'], 'prcs_id' => $data['PRCS_ID']));
                            } else {
                                $return['url'] = U('Flow/overLists');  //办理完毕返回已办结列表
                            }
                        } else {
                            $return['url'] = U('nextStep', array('run_id' => I('RUN_ID'), 'flow_id' => $data['FLOW_ID'], 'prcs_id' => $data['PRCS_ID']));
                        }
                    } else {
                        $return['info'] = '修改失败';
                    }
                } else {
                    $return['info'] = '请填写会签意见';
                }
            } else {
                //插入一条数据到flow_run表里
                $form_name = M('flow_form_type')->where('FORM_ID=' . $data['FORM_ID'])->getField('form_name'); //获取表单名
                $data['RUN_NAME'] = $form_name . '(' . date('Y-m-d H:i:s') . ')';
                $data['FLOW_ID'] = M('flow_type')->where('FORM_ID=' . $data['FORM_ID'])->getField('flow_id');
                $data['BEGIN_USER'] = session('user_id');
                $data['BEGIN_DEPT'] = session('dept_id');
                $data['BEGIN_TIME'] = date('Y-m-d H:i:s');
                $data['DEL_FLAG'] = 0;
                $run_id = M('flow_run')->order('RUN_ID DESC')->limit(1)->getField('run_id');
                $data['RUN_ID'] = $run_id + 1;
                $txt = 'RUN_NAME=' . $data['RUN_NAME'] . ' FLOW_ID=' . $data['FLOW_ID'] . ' BEGIN_USER=' . $data['BEGIN_USER'] . ' BEGIN_DEPT' . $data['BEGIN_DEPT'] . ' BEGIN_TIME' . $data['BEGIN_TIME'] . ' DEL_FLAG' . $data['DEL_FLAG'] . ' RUN_ID' . $data['RUN_ID'];
                $res = M('flow_run')->add($data);

                if ($res !== false) {
                    //添加flow_run_prcs表
                    $pData['RUN_ID'] = $data['RUN_ID'];
                    $pData['PRCS_ID'] = 1;
                    $pData['USER_ID'] = session('user_id');
                    $pData['PRCS_DEPT'] = session('dept_id');
                    $pData['PARENT_PRCS_ID'] = 0;   //上一步流程prcs_id
                    $pData['PRCS_TIME'] = date('Y-m-d H:i:s');
                    $pData['PRCS_FLAG'] = 2;    //办理中,如果转交下一步，此字段改为3，已转交
                    $pData['FLOW_PRCS'] = 1;
                    if ($pData['FLOW_PRCS'] > 1) {
                        $pData['PARENT'] = $pData['FLOW_PRCS'] - 1;
                    }
                    $pData['CREATE_TIME'] = date('Y-m-d H:i:s');
                    M('flow_run_prcs')->add($pData);

                    //添加flow_run_log表
                    $log_id = M('flow_run_log')->order('LOG_ID DESC')->limit(1)->getField('log_id');
                    $lData['LOG_ID'] = $log_id + 1;
                    $lData['RUN_ID'] = $data['RUN_ID'];
                    $lData['RUN_NAME'] = $data['RUN_NAME'];
                    $lData['FLOW_ID'] = $data['FLOW_ID'];
                    $lData['PRCS_ID'] = 1;
                    $lData['FLOW_PRCS'] = 1;
                    if ($lData['FLOW_PRCS'] > 1) {
                        $lData['PARENT'] = $lData['FLOW_PRCS'] - 1;
                    }
                    $lData['USER_ID'] = session('user_id');
                    $lData['TIME'] = date('Y-m-d H:i:s');
                    $lData['IP'] = gethostbyname($_ENV['COMPUTERNAME']);
                    $lData['TYPE'] = 1;
                    M('flow_run_log')->add($lData);

                    $new_run_id = $run_id + 1;
                    $runRow = M('flow_run')->where('RUN_ID=' . $new_run_id)->find();

                    foreach ($data as $key => $row) {
                        $newData[strtolower($key)] = $row;
                    }
                    $newData['run_id'] = $new_run_id;
                    $newData['run_name'] = $runRow['run_name'];
                    $newData['begin_user'] = session('user_id');
                    $newData['begin_time'] = date('Y-m-d H:i:s');
                    $table = 'flow_data_' . $runRow['flow_id'];
                    $dataRes = M($table)->add($newData);

                    if ($dataRes) {
                        if ($type) {
                            $return['info'] = '保存成功';
                        } else {
                            $return['url'] = U('nextStep', array('run_id' => $new_run_id, 'flow_id' => $data['FLOW_ID'], 'form_id' => $data['FORM_ID']));
                        }
                    } else {
                        $return['info'] = 'flow_run_data save fail';
                    }
                } else {
                    $return['info'] = 'save all fail';
                }
            }

            $this->ajaxReturn($return);
        }

        //点击提醒链接时，判断该流程当前用户是否已办结
        if (I('webid')) {
            $map['RUN_ID'] = I('run_id');
            $map['FLOW_PRCS'] = I('prcs_id');
            $map['USER_ID'] = session('user_id');
            $flag = M('flow_run_prcs')->where($map)->getField('prcs_flag');
            if ($flag == 3 || ($flag == 4)) {
                //跳转到showForm页面
                redirect('http://www.jsumt.com:88/index.php/Index/Index/showForm/id/' . I('id') . '/run_id/' . I('run_id') . '/prcs_id/' . I('prcs_id') . '/webid/' . $this->webid);
            }
        }

        unset($map);
        //获取表单
        $map['FORM_ID'] = I('id');
        $form = M('flow_form_type')->where($map)->find();
        $this->form = $form;

        //获取flow_id
        if (I('run_id')) {
            $flow_id = M('flow_run')->where('RUN_ID=' . I('run_id'))->getField('flow_id');
            $this->flow_id = $flow_id;
        }

        if (!I('run_id')) {
            $prcs_id = 1;
            $op = 1;
        } else {
            $prcs_id = I('prcs_id');

            //判断当前用户是否主办，主办=>转交下一步；经办=>办理完毕
            unset($map);
            $map['RUN_ID'] = I('run_id');
            if ($prcs_id) {
                $map['FLOW_PRCS'] = $prcs_id;
            }
            $map['USER_ID'] = session('user_id');
            $op = M('flow_run_prcs')->where($map)->getField('OP_FLAG');
            //判断当前步骤是否为会签 
            $pMap['FLOW_ID'] = $flow_id;
            $pMap['PRCS_ID'] = $prcs_id;
            $prcsName = M('flow_process')->where($pMap)->getField('prcs_name');
            if ($op == 1 && strpos($prcsName, '会签')) {
                $op = 0;
            } elseif ($op == 0 && strpos($prcsName, '会签')) {
                $op = 0;
            }

            //修改prcs_flag状态,当前用户为2，添加开始时间
            unset($map);
            $map['RUN_ID'] = I('run_id');
            $map['USER_ID'] = session('user_id');
            $map['PRCS_ID'] = I('currPrcs');
            $flag = M('flow_run_prcs')->where($map)->getField('prcs_flag');
            if ($flag == 1) {
                $data['PRCS_FLAG'] = 2;
                $data['PRCS_TIME'] = date('Y-m-d H:i:s');
                $data['MOBILE_FLAG'] = 1;
                M('flow_run_prcs')->where($map)->save($data);   //当前用户状态改为2
            }
            unset($map['USER_ID']);
            unset($map['PRCS_ID']);
            $map['PRCS_ID'] = I('currPrcs') - 1;
            M('flow_run_prcs')->where($map)->save(array('PRCS_FLAG' => 4));   //上一步骤用户状态改为4
            //获取手机端上传的附件
            $info = M('flow_run')->where('RUN_ID=' . I('run_id'))->find();
            //$info = M('flow_run')->where('RUN_ID=81')->find();

            /*
             * 2018/3/22  增加第一步表单未转交后的修改功能
             * form.js 自动加载方法通过flow_id是否存在判断是否可修改
             */
            if (I('currPrcs') == 1) {
                $info['flow_id'] = 0;
                $info['isEdit'] = 'edit';
            }

            //获取PC端上传的文件
            $uploads = downloadFile($info['attachment_id'],$info['attachment_name'],'workflow',$info['uploads'],$info['uploads_url']);
            
            $this->uploads = $uploads;
            $this->info = $info;
            // print_r($info);
        }
        //echo $op;
//        echo $prcs_id;
        $this->prcs = $prcs_id;
        $this->currPrcs = I('currPrcs');    //当前流程步骤标识
        //   $this->prcs = 1;  //测试会签意见区
        $this->op = $op;



        //获取当前部门
        $this->department = getDeptName(session('dept_id'));
        $this->form_id = I('id');
        $this->run_id = I('run_id');
        $this->display();
    }

    //显示已办流程页面
    public function showForm() {
        //获取表单
        $map['FORM_ID'] = I('id');
        $form = M('flow_form_type')->where($map)->find();
        $this->form = $form;

        $prcs_id = I('prcs_id');

        //获取手机端上传的附件
        $info = M('flow_run')->where('RUN_ID=' . I('run_id'))->find();
        // $info = M('flow_run')->where('RUN_ID=50')->find();
        //获取PC端文件
        $uploads = downloadFile($info['attachment_id'], $info['attachment_name'], 'workflow', $info['uploads'], $info['uploads_url']);

        $this->uploads = $uploads;
        $this->info = $info;

        //获取会签内容
        unset($map);
        $map['RUN_ID'] = I('run_id');
        $feeds = M('flow_run_feedback')->where($map)->order('PRCS_ID')->select();
        foreach ($feeds as $key => $row) {
            $pMap['FLOW_ID'] = $info['flow_id'];
            $pMap['PRCS_ID'] = $row['prcs_id'];
            $feeds[$key]['prcs_name'] = M('flow_process')->where($pMap)->getField('prcs_name');
            $feeds[$key]['content'] = strip_tags($row['content']);
        }
        $this->feeds = $feeds;

        //获取流程图
        $runPrcs = M('flow_run_prcs')->field('RUN_ID,USER_ID,PRCS_TIME,DELIVER_TIME,PRCS_FLAG,FLOW_PRCS')->where($map)->order('PRCS_ID')->select();
        $sql = 'select d.PRCS_NAME,d.PRCS_ID,c.PRCS_ID as id,c.PRCS_FLAG from flow_process d , (select a.FLOW_id,b.PRCS_ID,b.FLOW_PRCS,b.PRCS_FLAG from flow_run a,flow_run_prcs b where a.RUN_ID = b.RUN_ID and a.RUN_ID = ' . I('run_id') . ' order by b.PRCS_ID asc) as c where d.FLOW_ID = c.FLOW_id and d.PRCS_ID = c.FLOW_PRCS';
        $arr = M()->query($sql);
        foreach ($arr as $key => $row) {
            $process[$row['id']] = $row;
        }
        foreach ($process as $k1 => $r1) {
            foreach ($runPrcs as $k2 => $r2) {
                if ($r2['prcs_time'] && $r2['deliver_time']) {
                    $r2['dec'] = timediff(strtotime($r2['deliver_time']), strtotime($r2['prcs_time']));
                }
                if (($r1['prcs_id'] == $r2['flow_prcs']) && ($r1['prcs_flag'] == $r2['prcs_flag'])) {
                    $process[$k1]['data'][] = $r2;
                }
                //有些流程k1!=r2[flow_prcs]，会导致有的步骤不显示 18/5/7改回原来的
//                if ($k1 == $r2['flow_prcs']) {
//                    $process[$k1]['data'][] = $r2;
//                }
            }
        }
//          print_r($process);
        $this->process = $process;

        //获取flow_id
        $this->flow_id = M('flow_run')->where('RUN_ID=' . I('run_id'))->getField('flow_id');
        $this->form_id = I('id');
        $this->run_id = I('run_id');
        $this->prcs = $prcs_id;
        $this->display();
    }

    //获取表单值
    public function getRunData() {
        $run_id = I('run_id');
        $flow_id = I('flow_id');

        if (I('type') == 'feedback') {
            //获取会签意见
            $map['RUN_ID'] = $run_id;
            $datas = M('flow_run_feedback')->where($map)->order('PRCS_ID')->select();
            foreach ($datas as $key => $row) {
                $datas[$key]['user_name'] = getChineseName($row['user_id']);
                $datas[$key]['content'] = strip_tags($row['content']);
            }
        } else {
            //$data = M('flow_run_data')->where('RUN_ID=' . $run_id)->select();
            //9.27升级后修改
            $data = M('flow_data_' . $flow_id)->where('run_id=' . $run_id)->find();
            foreach ($data as $key => $row) {
                if (strpos($key, 'data_') !== false) {
                    $datas['form'][] = $row;
                }
            }
            //获取会签意见
            $map['RUN_ID'] = $run_id;
            $feeds = M('flow_run_feedback')->field('USER_ID,PRCS_ID,CONTENT,EDIT_TIME')->where($map)->order('PRCS_ID')->select();
            foreach ($feeds as $key => $row) {
                $feeds[$key]['user_name'] = getChineseName($row['user_id']);
                $pMap['FLOW_ID'] = $flow_id;
                $pMap['PRCS_ID'] = $row['prcs_id'];
                $feeds[$key]['prcs_name'] = M('flow_process')->where($pMap)->getField('prcs_name');
                $content = str_replace('<p>', ' ', $row['content']);
                $content = str_replace('</p>', ' ', $content);
                $feeds[$key]['content'] = strip_tags($content);
            }
            $datas['feed'] = $feeds;
        }
        $datas['flow_id'] = $flow_id;   //因修改表单时flow_id手动改为0，js中动态加上，否则修改表单页面无法转交下一步
        $this->ajaxReturn($datas);
    }

    //转交下一步骤
    public function nextStep() {
        if (IS_AJAX && isset($_POST)) {
            $data = I('post.');

            //当前用户流程信息
            $one = M('flow_run_prcs')->where('RUN_ID=' . $data['run_id'])->order('PRCS_ID DESC')->limit(1)->find();

            //发送提示消息
//            vendor('Mpwexin');
//            $mp_weixin = new \Mpwexin();
//            $web = M('web')->where('id=' . session('webid'))->find();
            $flow_name = M('flow_run')->where(array('RUN_ID' => $data['run_id'], 'FLOW_ID' => $data['flow_id']))->getField('run_name');
            $form = M('flow_type')->field('FLOW_NAME,FORM_ID')->where(array('FLOW_ID' => $data['flow_id']))->find();
            //获取流程发起人
            $uMap['RUN_ID'] = $data['run_id'];
            $uMap['FLOW_PRCS'] = 1;
            $creator = M('flow_run_prcs')->where($uMap)->getField('user_id');
            $currPrcs = $one['prcs_id'] + 1;
            $content = "您有一条新流程请尽快办理，\n流程名：<a href=\"http://www.jsumt.com:88/index.php/Index/Index/form/id/" . $form['form_id'] . "/run_id/" . $data['run_id'] . "/prcs_id/" . $currPrcs . "/webid/" . $this->webid . "\">" . $flow_name . "，发起人：" . getChineseName($creator) . "</a>，转交人：" . getChineseName(session('user_id'));

            //往flow_run_prcs里添加数据，每个经办人一条
            $pData['RUN_ID'] = $data['run_id'];
            $pData['PRCS_ID'] = $one['prcs_id'] + 1;
            $pData['PRCS_FLAG'] = 1;    //未接收
            $pData['FLOW_PRCS'] = $data['prcs_id'];
            $pData['PARENT'] = $one['flow_prcs'];
            $pData['CREATE_TIME'] = date('Y-m-d H:i:s');
            $pData['PARENT_PRCS_ID'] = $one['prcs_id'];

            //去重条件
            $dMap['RUN_ID'] = $data['run_id'];
            $dMap['PRCS_ID'] = $one['prcs_id'] + 1;

            //只有主办人
            if ($data['user_id'] && !$data['user_ids']) {
                //去重
                $dMap['USER_ID'] = $data['user_id'];
                $has = M('flow_run_prcs')->where($dMap)->count();
                if (!$has) {
                    $pData['USER_ID'] = $data['user_id'];
                    $pData['PRCS_DEPT'] = M('user')->where('USER_ID="' . $data['user_id'] . '"')->getField('dept_id');
                    $pData['OP_FLAG'] = 1;
                    $resMain = M('flow_run_prcs')->add($pData);

                    //$user = getWxUserid($data['user_id']);
                    //$mp_weixin->setMessage($web['wx_corpid'], $web['wx_appsecret'], $user, $content, $agentid = '');
//                    setWeixinMsg($data['user_id'], $content, 1);
                }
            } else if ($data['user_ids'] && !$data['user_id']) {
                //只有经办人
                foreach ($data['user_ids'] as $key => $row) {
                    //去重
                    $dMap['USER_ID'] = $row;
                    $has = M('flow_run_prcs')->where($dMap)->count();
                    if (!$has) {
                        $pData['USER_ID'] = $row;
                        $pData['PRCS_DEPT'] = M('user')->where('USER_ID="' . $row . '"')->getField('dept_id');
                        $pData['OP_FLAG'] = 0;
                        $resOther = M('flow_run_prcs')->add($pData);

//                        $user = getWxUserid($row);
//                        $mp_weixin->setMessage($web['wx_corpid'], $web['wx_appsecret'], $user, $content, $agentid = '');
//                        setWeixinMsg($row, $content, $webid = '');
                    }
                }
            } else if ($data['user_id'] && $data['user_ids']) {
                //两种人都有
                array_push($data['user_ids'], $data['user_id']);
                $data['user_id'] = array_unique($data['user_ids']);
                foreach ($data['user_id'] as $key => $row) {
                    //去重
                    $dMap['USER_ID'] = $row;
                    $has = M('flow_run_prcs')->where($dMap)->count();
                    if (!$has) {
                        $pData['USER_ID'] = $row;
                        $pData['PRCS_DEPT'] = M('user')->where('USER_ID="' . $row . '"')->getField('dept_id');
                        if ($key == 0) {
                            $pData['OP_FLAG'] = 1;
                        } else {
                            $pData['OP_FLAG'] = 0;
                        }
                        $resAll = M('flow_run_prcs')->add($pData);

//                        $user = getWxUserid($row);
//                        $mp_weixin->setMessage($web['wx_corpid'], $web['wx_appsecret'], $user, $content, $agentid = '');
                    }
                }
                setWeixinMsg($data['user_id'], $content, $webid = '');
            }

            if ($resMain || $resOther || $resAll) {
                //修改上一步骤标记和完成时间
//                $map['RUN_ID'] = $data['run_id'];
//                $map['PRCS_ID'] = $one['prcs_id'];
//                $saveData['DELIVER_TIME'] = date('Y-m-d H:i:s');
//                $saveData['PRCS_FLAG'] = 3;
//                $res = M('flow_run_prcs')->where($map)->save($saveData);
                //是否为会签
                $fMap['FLOW_ID'] = $data['flow_id'];
                $fMap['PRCS_ID'] = $data['over_prcs_id'];
                $prcsName = M('flow_process')->where($fMap)->getField('prcs_name');

                $rMap['RUN_ID'] = $data['run_id'];
                $rMap['FLOW_PRCS'] = $data['over_prcs_id'];
                if (strpos($prcsName, '会签') !== false) {
                    $rMap['USER_ID'] = $data['over_user'];
                }
                $rData['DELIVER_TIME'] = date('Y-m-d H:i:s');
                $rData['PRCS_FLAG'] = 3;
                M('flow_run_prcs')->where($rMap)->save($rData);
            }

            if ($res !== false) {
                //修改log表（上一步骤）
                unset($map);
                $map['RUN_ID'] = $data['run_id'];
                $map['FLOW_ID'] = $data['flow_id'];
                $map['PRCS_ID'] = $one['prcs_id'];
                $map['FLOW_PRCS'] = $one['flow_prcs'];
                $names = implode(',', $data['user_names']);
                $sData['CONTENT'] = '转交步骤' . $pData['FLOW_PRCS'] . '，办理人：' . $names;
                M('flow_run_log')->where($map)->save($sData);

                //添加log表（转交后的步骤）
                $lData['RUN_ID'] = $data['run_id'];
                $lData['RUN_NAME'] = M('flow_run')->where(array('RUN_ID' => $data['run_id'], 'FLOW_ID' => $data['flow_id']))->getField('run_name');
                $lData['FLOW_ID'] = $data['flow_id'];
                $lData['PRCS_ID'] = $pData['PRCS_ID'];
                $lData['FLOW_PRCS'] = $pData['FLOW_PRCS'];
                //获取转交后步骤的用户名
                $uMap['RUN_ID'] = $data['run_id'];
                $uMap['FLOW_PRCS'] = $data['prcs_id'];
                $uMap['PRCS_FLAG'] = 1;
                $lData['USER_ID'] = session('user_id');
                $lData['TIME'] = date('Y-m-d H:i:s');
                $lData['IP'] = gethostbyname($_ENV['COMPUTERNAME']);
                $lData['TYPE'] = 1;
                $res = M('flow_run_log')->add($lData);
            }

            /*
             * 关联和考勤相关的流程
             */
            //取相关的flow_id
            unset($where);
            $where['_string'] = ' FLOW_NAME like "请假申请%" or FLOW_NAME like "出差申请%" or FLOW_NAME like "外出申请%" or FLOW_NAME like "加班申请%"';
            $flow_ids = M('flow_type')->where($where)->getField('flow_id', true);

            if (in_array($data['flow_id'], $flow_ids) && ($one['flow_prcs'] == 2)) {
                //取表单项数据
                $flow_data = M('flow_data_' . $data['flow_id'])->where('RUN_ID=' . $data['run_id'])->find();

                switch ($data['flow_id']) {
                    case 146:   //请假
                        $checkRow = M('attend_leave')->where('run_id=' . $flow_data['run_id'])->count();
                        if (!$checkRow) {
                            $attData['USER_ID'] = $flow_data['begin_user'];
                            $attData['LEAVE_TYPE2'] = $flow_data['data_35'];
                            $attData['LEAVE_DATE1'] = $flow_data['data_56'];
                            $attData['LEAVE_DATE2'] = $flow_data['data_57'];
                            $attData['LEAVE_TYPE'] = $flow_data['data_63'];
                            $attData['ALLOW'] = 0;
                            $attData['LEAVE_DURATION'] = $flow_data['data_66'];
                            $attData['run_id'] = $flow_data['run_id'];
                            $res = M('attend_leave')->add($attData);

                            //扣除对应年假
                            if ($res) {
                                switch ($flow_data['data_35']) {
                                    case '年假':
                                        $ableYear = M('userinfo')->where('user_id="' . $flow_data['begin_user'] . '"')->getField('able_year');
                                        $infoData['able_year'] = $ableYear - floatval($flow_data['data_66']);
                                        break;
                                    case '调休':
                                        $ableVacation = M('userinfo')->where('user_id="' . $flow_data['begin_user'] . '"')->getField('able_vacation');
                                        $infoData['able_vacation'] = $ableVacation - floatval($flow_data['data_66']);
                                        break;
                                }
                                $infoMap['user_id'] = $flow_data['begin_user'];
                                M('userinfo')->where($infoMap)->save($infoData);
                            }
                        }
                        break;
                    case 163:   //外出
                        $checkRow = M('attend_out')->where('run_id=' . $flow_data['run_id'])->count();
                        if (!$checkRow) {
                            $attData['USER_ID'] = $flow_data['begin_user'];
                            $attData['CREATE_DATE'] = $flow_data['begin_time'];
                            $attData['SUBMIT_TIME'] = substr($flow_data['data_56'], 0, 10);
                            $attData['OUT_TIME1'] = substr($flow_data['data_56'], 11, 5);
                            $attData['OUT_TIME2'] = substr($flow_data['data_57'], 11, 5);
                            $attData['ALLOW'] = 0;
                            $attData['STATUS'] = 0;
                            $attData['OUT_TYPE'] = $flow_data['data_53'];
                            $attData['run_id'] = $flow_data['run_id'];
                            $attData['time_long'] = $flow_data['data_52'];
                            M('attend_out')->add($attData);
                        }
                        break;
                    case 150:   //出差
                        $checkRow = M('attend_evection')->where('run_id=' . $flow_data['run_id'])->count();
                        if (!$checkRow) {
                            $attData['USER_ID'] = $flow_data['begin_user'];
                            $attData['EVECTION_DEST'] = $flow_data['data_63'];
                            $attData['EVECTION_DATE1'] = $flow_data['data_55'];
                            $attData['EVECTION_DATE2'] = $flow_data['data_56'];
                            $attData['ALLOW'] = 1;
                            $attData['STATUS'] = 1;
                            $attData['REASON'] = $flow_data['data_62'];
                            $attData['run_id'] = $flow_data['run_id'];
                            M('attend_evection')->add($attData);
                        }
                        break;
                    case 145:   //加班
                        $checkRow = M('attendance_overtime')->where('run_id=' . $flow_data['run_id'])->count();
                        if (!$checkRow) {
                            $attData['USER_ID'] = $flow_data['begin_user'];
                            $attData['RECORD_TIME'] = $flow_data['begin_time'];
                            $attData['START_TIME'] = $flow_data['data_57'];
                            $attData['END_TIME'] = $flow_data['data_58'];
                            $attData['OVERTIME_HOURS'] = $flow_data['data_60'];
                            $attData['OVERTIME_CONTENT'] = $flow_data['data_51'];
                            $attData['ALLOW'] = 1;
                            $attData['STATUS'] = 0;
                            $attData['run_id'] = $flow_data['run_id'];
                            M('attendance_overtime')->add($attData);
                        }
                        break;
                }
            }


            if ($res !== false) {
                $return['info'] = '转交成功';
                $return['url'] = U('Flow/lists');
            } else {
                $return['info'] = '转交失败';
            }
            $this->ajaxReturn($return);
        }

        $run_id = I('run_id');
        $flow_id = I('flow_id');
        $prcs_id = I('prcs_id') ? I('prcs_id') : 1;
        //获取流程信息
        $runs = M('flow_run')->where(array('RUN_ID' => $run_id))->find();
        //添加流程名及表单id
        $flowType = M('flow_type')->field('FLOW_NAME,FORM_ID')->where(array('FLOW_ID' => $flow_id))->find();
        $runs['flow_name'] = $flowType['flow_name'];
        $runs['form_id'] = $flowType['form_id'];
        //获取该流程步骤
        $steps = M('flow_process')->field('PRCS_ID,PRCS_NAME,PRCS_DEPT,PRCS_PRIV,PRCS_USER,PRCS_TO,FEEDBACK,PRCS_IN,PRCS_IN_SET,ALLOW_BACK')->where(array('FLOW_ID' => $flow_id))->order('PRCS_ID')->select();
        foreach ($steps as $key => $row) {
            if ($row['prcs_id'] == $prcs_id) {
                $map['RUN_ID'] = $run_id;
                $map['FLOW_PRCS'] = $row['prcs_id'];
                $users = M('flow_run_prcs')->field('USER_ID,FLOW_PRCS,PRCS_FLAG')->where($map)->select();
                $steps[$key]['user'] = $users;
            }
        }
        $runs['step'] = $steps;
        $this->runs = $runs;


        //print_r($form_data);die;
        //获取下一步骤
        unset($map);
        $map['FLOW_ID'] = $flow_id;
        $map['PRCS_ID'] = $prcs_id;
        $prcs_to = M('flow_process')->field('PRCS_ID,PRCS_NAME')->where($map)->getField('PRCS_TO');
        if (!$prcs_to) {
            $map['PRCS_ID'] = $prcs_id + 1;
        } else {
            $prcs_to = rtrim($prcs_to, ',');
            $prcs_to = explode(',', $prcs_to);
            $map['PRCS_ID'] = array('in', $prcs_to);
        }

        $flag = 0;
        $nexts = M('flow_process')->field('PRCS_ID,PRCS_NAME,PRCS_DEPT,PRCS_PRIV,PRCS_USER,PRCS_TO,AUTO_TYPE,AUTO_USER,USER_FILTER,TOP_DEFAULT,FEEDBACK,PRCS_IN,PRCS_IN_SET,ALLOW_BACK,AUTO_TYPE,CONDITION_DESC')->where($map)->select();
        $sort = array(
            'direction' => 'SORT_ASC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序  
            'field' => 'prcs_id', //排序字段  
        );
        $arrSort = array();
        foreach ($nexts AS $uniqid => $row) {
            foreach ($row AS $key => $value) {
                $arrSort[$key][$uniqid] = $value;
            }
        }
        array_multisort($arrSort[$sort['field']], constant($sort['direction']), $nexts);
        $count = count($nexts);
        $num = 0;
        if ($nexts[0]) {
            $table = 'flow_data_' . $flow_id;

            //获取form表单中的key
            $html = M('flow_form_type')->where('FORM_ID=' . $runs['form_id'])->getField('print_model');
            preg_match_all('/[^s]name[\s]*?=[\s]*?([\'\"])(.*?)\1/', $html, $match);
            $htmlName = $match[2];
            preg_match_all('/title[\s]*?=[\s]*?([\'\"])(.*?)\1/', $html, $match);
            $htmlTitle = $match[2];
            for ($i = 0; $i < count($htmlTitle); $i++) {
                if (strpos($htmlName[$i], 'OTHER') === false) {
                    $htmlArr[strtolower($htmlName[$i])] = $htmlTitle[$i];
                }
            }
            foreach ($nexts as $key => $row) {
                $tmp_data = M($table)->where('run_id=' . $run_id)->find();
                foreach ($tmp_data as $t_k => $t_v) {
                    if (strpos($t_k, 'data_') !== false) {
                        $form_data[$t_k] = $t_v;
                    }
                }
//                print_r($form_data);
                if ($row['prcs_in']) {
                    $PRCS_IN_DESC = str_replace("\n", "<br>", $row['prcs_in']);
                    $PRCS_IN_DESC = str_replace("'", " ", $PRCS_IN_DESC);
//                    echo $PRCS_IN_DESC;
                    $sql = getFormCondition($PRCS_IN_DESC, $form_data, $prcs_id, $row['prcs_in_set'], $htmlArr);
//                    echo $sql;
                    preg_match_all('/\[.*like|not like\%/U', $sql, $arr);
                    foreach ($arr[0] as $ak => $av) {
                        $last = strripos($av, '[');
                        $new[] = substr($av, $last);
                    }
                    preg_match_all('/(?:%)(.*)(?:%)/U', $sql, $arr1);
                    $params = $arr1[1];
                    $tmp1 = array();
                    foreach ($new as $k => $v) {
                        $v = explode(']', $v);
                        $v_k = trim($v[0], '[');
                        $v_v = trim($v[1]);
                        $v[0] = $v[0] . ']';
                        if ($v[0] == '[CUR_USER_DEPT]' || ($v[0] == '[CUR_USER_PRIV]') || ($v[0] == '[CUR_USER_PRIV_OTHER]')) {
                            //$tmp1[$v_v] = $v_k;
                            $tmp1[][$v_v] = $v_k;
                        }
                    }

                    $params = getDartPrivId($tmp1, $params);
//                  var_dump($params);die;
                    $nexts[$key]['sql'] = $sql;
                    if (strpos($sql, '[CUR_USER_DEPT]') !== false) {
                        foreach ($tmp1 as $t_key => $t_val) {
                            $tempvalue = array_values($t_val);
                            $tempkey = key($t_val);

                            if ($tempvalue[0] == 'CUR_USER_DEPT') {
                                //$sql = str_replace('[当前主办人部门]', 'c.DEPT_ID', $sql);
                                $sql = preg_replace("/%.+部%/is", '%' . trim($params[$t_key]) . '%', $sql);
                                $sql_0 = str_replace('[CUR_USER_DEPT]', 'c.DEPT_ID', $sql);
                                $sql_1 = str_replace('[CUR_USER_DEPT]', 'c.DEPT_ID_OTHER', $sql);
                                $sql_2 = strstr($sql_1, 'c');
                                if ($tempkey == 'not like')
                                    $sql = $sql_0 . " and " . $sql_2;
                                elseif ($tempkey == 'like')
                                    $sql = $sql_0 . " or " . $sql_2;
                            }
                        }
                    }

                    if (strpos($sql, '[CUR_FLOW_PRCS]') !== false) {
                        $sql = str_replace('[CUR_FLOW_PRCS]', 'b.FLOW_PRCS', $sql);
                    }

                    if (strpos($sql, '[CUR_USER_PRIV_OTHER]') !== false) {
                        foreach ($tmp1 as $t_key2 => $t_val2) {
                            $tempvalue = array_values($t_val2);
                            $tempkey = key($t_val2);

                            if ($tempvalue[0] == 'CUR_USER_PRIV_OTHER') {

                                $sql = str_replace('[CUR_USER_PRIV_OTHER]', 'c.USER_PRIV_OTHER', $sql);
                                $sql = preg_replace('/[\x7f-\xff]+/', ltrim($params[$t_key2]), $sql);
                            }
                        }
                    }
                    if (strpos($sql, '[CUR_USER_PRIV]') !== false) {
                        foreach ($tmp1 as $t_key1 => $t_val1) {
                            $tempvalue = array_values($t_val1);
                            $tempkey = key($t_val1);

                            if ($tempvalue[0] == 'CUR_USER_PRIV') {
                                $sql = str_replace('[CUR_USER_PRIV]', 'c.USER_PRIV', $sql);
                                $sql = preg_replace('/[\x7f-\xff]+/', trim($params[$t_key1]), $sql);
                            }
                        }
                    }
                    //echo $sql, '<br/>';
                    //判断条件是否成立，自动选择对应流程
                    if ($sql) {
                        $sql = 'select * from ' . $table . ' as a left join flow_run_prcs as b on a.run_id = b.RUN_ID left join  user as c on c.USER_ID = b.USER_ID where a.run_id=' . $run_id . ' and (' . $sql . ')';
                        //$sql = 'select * from flow_run_prcs as b left join user as c on b.USER_ID = c.USER_ID where b.RUN_ID= '. $run_id . ' and (' . $sql . ') and b.PRCS_ID = '.$prcs_id;
//                         echo $sql,'<hr/>';
                        $result = M()->query($sql);

                        if ($result) {
                            $one = 1;
                        } else {
                            $one = 0;
                        }
                    }

                    if ($one) {
                        $nexts[$key]['one'] = 1;
                        $auto_type = $row['auto_type'] ? $row['auto_type'] : 0;
                        $flag ++;
                        $num = 1;
                        break;
                    } else {
                        $nexts[$key]['one'] = 0;
                    }
                } elseif ($count == 1) {
                    $nexts[$key]['one'] = 1;
                    $auto_type = $row['auto_type'] ? $row['auto_type'] : 0;
                    $flag ++;
                    $num = 1;
                } else {
                    $nexts[$key]['one'] = 1;
                    $auto_type = $row['auto_type'] ? $row['auto_type'] : 0;
                    $flag ++;
                    $num = 1;
                    //                    break;
                }
            }
        }

        if (in_array('0', $prcs_to) && $num == 0) {
            $nexts = array();
        }
//        print_r($nexts);
        //获取下一步经办人
//        $operators = $this->getData($flow_id, $row['prcs_id'], $run_id);
//        $count = count($operators);
//
//        $this->count = $count;
//        //添加步驟名和是否会签标记到$operators數組中
//        $operators = addPrcsname($operators, $flow_id, $prcs_id);

        $this->nexts = $nexts;
        $this->flag = $flag;
        $this->flow_id = $flow_id;
        $this->prcs_id = $prcs_id;
        $this->run_id = $run_id;
        $this->auto_type = $auto_type;
        $this->form_id = $runs['form_id'];
        $this->current_user = session('user_id');
        $this->display();
    }

    //结束流程
    public function overFlow() {
        $data = I('post.');
        //修改prcs表状态=4
        $map['RUN_ID'] = $data['run_id'];
        $map['FLOW_PRCS'] = $data['over_prcs_id'];
        $sdata['PRCS_FLAG'] = 4;
        $sdata['DELIVER_TIME'] = date('Y-m-d H:i:s');
        $res = M('flow_run_prcs')->where($map)->save($sdata);

        if ($res !== false) {
            //增加flow_run表end_time字段
            $rMap['RUN_ID'] = $data['run_id'];
            $rData['END_TIME'] = date('Y-m-d H:i:s');
            M('flow_run')->where($rMap)->save($rData);

            //添加日志
            unset($sdata);
            $map['USER_ID'] = session('user_id');
            $row = M('flow_run_prcs')->where($map)->find();
            $sdata['RUN_ID'] = $data['run_id'];
            $sdata['FLOW_ID'] = $data['flow_id'];
            $sdata['RUN_NAME'] = M('flow_form_type')->where(array('FORM_ID' => $data['form_id']))->getField('form_name') . '(' . date('Y-m-d H:i:s') . ')';
            $sdata['PRCS_ID'] = $row['prcs_id'];
            $sdata['FLOW_PRCS'] = $row['flow_prcs'];
            $sdata['USER_ID'] = $row['user_id'];
            $sdata['TIME'] = date('Y-m-d H:i:s');
            $sdata['IP'] = gethostbyname($_ENV['COMPUTERNAME']);
            $sdata['TYPE'] = 1;
            $sdata['CONTENT'] = '结束流程';
            M('flow_run_log')->add($sdata);

            //修改当前步骤其他用户日志
            unset($map);
            unset($sdata);
            $map['RUN_ID'] = $data['run_id'];
            $map['FLOW_PRCS'] = $row['flow_prcs'];
            $map['FLOW_ID'] = $data['flow_id'];
            $sdata['CONTENT'] = '结束流程';
            $res = M('flow_run_log')->where($map)->save($sdata);
        }

        $return['url'] = $res !== false ? U('Flow/overLists') : '';
        $return['info'] = $res !== false ? '流程办理成功' : '流程办理失败';
        $this->ajaxReturn($return);
    }

    public function getFlowUser() {
        $flow_id = I('flow_id');
        $prcs_id = I('prcs_id');
        $run_id = I('run_id');
        $auto_type = I('auto_type');
        $data = $this->getData($flow_id, $prcs_id, $run_id, $auto_type);

        //添加步驟名和是否会签标记到$data數組中
        $data = addPrcsname($data, $flow_id, $prcs_id);
        foreach ($data as $key => $row) {
            $data[$key]['auto_type'] = $auto_type;
        }

        $this->ajaxReturn($data);
    }

    //获取部门下的员工
    public function getUserData() {
        $dept_id = I('dept_id');
        $flow_id = I('flow_id');
        $run_id = I('run_id');
        $prcs_id = I('prcs_id');
        $auto_type = I('auto_type');

        $data = $this->getData($flow_id, $prcs_id, $run_id, $auto_type, $dept_id, 30);

        $this->ajaxReturn($data);
    }

    public function upload() {
        $this->upfile('./Public/Uploads/files/');
    }

//    public function delFile() {
//        $url = I('url');
//        if ($url) {
//            $res = @unlink('.' . $url);
//        }
//        //$return['status'] = $res !== false ? true : false;
//        $return['status'] = true;
//        $this->ajaxReturn($return);
//    }
    //回退
    public function backFlow() {
        $data = I('get.');

        //获取上一步信息并修改状态
        $prveMap['RUN_ID'] = $data['run_id'];
        $prveMap['PRCS_ID'] = $data['current_prcs_id'] - 1;
        $prveData['PRCS_FLAG'] = 4;
        M('flow_run_prcs')->where($prveMap)->save($prveData);

        //获取回退的步骤信息
        $map['RUN_ID'] = $data['run_id'];
        $map['FLOW_PRCS'] = $data['flow_prcs'];
        $row = M('flow_run_prcs')->where($map)->find();

        //添加flow_run_prcs表
        $fData['RUN_ID'] = $data['run_id'];
        $fData['PRCS_ID'] = $data['current_prcs_id'] + 1;
        $fData['USER_ID'] = $row['user_id'];
        $fData['PRCS_TIME'] = date('Y-m-d H:i:s');
        $fData['PRCS_FLAG'] = 1;
        $fData['FLOW_PRCS'] = $row['flow_prcs'];
        $fData['OP_FLAG'] = $row['op_flag'];
        $fData['TOP_FLAG'] = $row['top_flag'];
        $fData['CREATE_TIME'] = date('Y-m-d H:i:s');
        $fData['PRCS_DEPT'] = $row['prcs_dept'];
        $fData['PARENT'] = $data['current_flow_prcs'];
        $fData['PARENT_PRCS_ID'] = $data['current_prcs_id'];
        $fRes = M('flow_run_prcs')->add($fData);

        //修改flow_run_prcs当前步骤
        unset($map);
        $map['RUN_ID'] = $data['run_id'];
        $map['PRCS_ID'] = $data['current_prcs_id'];     //当前流程标识(唯一)
        $sData['DELIVER_TIME'] = date('Y-m-d H:i:s');
        $sData['BACK_PRCS_ID'] = $data['prcs_id'];
        $sData['BACK_FLOW_PRCS'] = $data['flow_prcs'];
        $sData['PRCS_FLAG'] = 0;
        $sData['MOBILE_FLAG'] = 1;
        $fsRes = M('flow_run_prcs')->where($map)->save($sData);

        //添加flow_run_log表
        $run = M('flow_run')->where(array('RUN_ID' => $data['run_id']))->find();
        $lData['RUN_ID'] = $data['run_id'];
        $lData['RUN_NAME'] = $run['run_name'];
        $lData['FLOW_ID'] = $run['flow_id'];
        $lData['PRCS_ID'] = $data['current_prcs_id'] + 1;
        $lData['FLOW_PRCS'] = $row['flow_prcs'];
        $lData['USER_ID'] = $row['user_id'];
        $lData['TIME'] = date('Y-m-d H:i:s');
        $lData['IP'] = gethostbyname($_ENV['COMPUTERNAME']);
        $lData['TYPE'] = 1;
        $lRes = M('flow_run_log')->add($lData);

        //修改当前步骤log
        unset($map);
        unset($sData);
        $map['RUN_ID'] = $data['run_id'];
        $map['PRCS_ID'] = $data['current_prcs_id'];
        $sData['CONTENT'] = '回退到第' . $data['prcs_id'] . '步';
        $lsRes = M('flow_run_log')->where($map)->save($sData);

        if ($fRes) {
            $return['info'] = '回退成功';
            $return['url'] = U('Flow/overLists');

            //发送退回提醒
            $form = M('flow_type')->where('FLOW_ID=' . $run['flow_id'])->find();
            //发起人
            $creator = M('flow_run_prcs')->where(array('RUN_ID' => $row['run_id'], 'FLOW_PRCS' => 1))->getField('user_id');
            $currPrcs = $data['current_prcs_id'] + 1;
            $content = "您有一条流程被退回，请尽快办理，\n流程名：<a href=\"http://www.jsumt.com:88/index.php/Index/Index/form/id/" . $form['form_id'] . "/run_id/" . $data['run_id'] . "/prcs_id/" . $currPrcs . "/webid/" . $this->webid . "\">" . $run['run_name'] . "，发起人：" . getChineseName($creator) . "</a>，转交人：" . getChineseName(session('user_id'));
            setWeixinMsg($row['user_id'], $content, $this->webid);

            /*
             * 修改和考勤相关的数据
             * 删除请假表记录
             */
            $flow_id = M('flow_run')->where('RUN_ID=' . $data['run_id'])->getField('flow_id');

            $where['_string'] = ' FLOW_NAME like "请假申请%" or FLOW_NAME like "出差申请%" or FLOW_NAME like "外出申请%" or FLOW_NAME like "加班申请%"';
            $flow_ids = M('flow_type')->where($where)->getField('flow_id', true);

            if ($data['flow_prcs'] == 1) {  //回退到第一步时判断
                switch ($flow_id) {
                    case 146:       //请假流程 
                        // 获取表单数据
                        $flow_data = M('flow_data_' . $flow_id)->where('RUN_ID=' . $data['run_id'])->find();
                        switch ($flow_data['data_35']) {
                            case '年假':
                                $ableYear = M('userinfo')->where('user_id="' . $flow_data['begin_user'] . '"')->getField('able_year');
                                $infoData['able_year'] = $ableYear + floatval($flow_data['data_66']);
                                break;
                            case '调休':
                                $ableVacation = M('userinfo')->where('user_id="' . $flow_data['begin_user'] . '"')->getField('able_vacation');
                                $infoData['able_vacation'] = $ableVacation + floatval($flow_data['data_66']);
                                break;
                        }

                        $countNum = M('attend_leave')->where('RUN_ID=' . $data['run_id'])->count();
                        if ($countNum > 0) {    //如果该流程id存在，则删除
                            $infoMap['user_id'] = $flow_data['begin_user'];
                            M('userinfo')->where($infoMap)->save($infoData);
                            M('attend_leave')->where('RUN_ID=' . $data['run_id'])->delete();
                        }
                        break;
                    case 163:   //外出
                        $countNum = M('attend_out')->where('RUN_ID=' . $data['run_id'])->count();
                        if ($countNum > 0) {
                            M('attend_out')->where('RUN_ID=' . $data['run_id'])->delete();
                        }
                        break;
                    case 150:       //出差
                        $countNum = M('attend_evection')->where('RUN_ID=' . $data['run_id'])->count();
                        if ($countNum > 0) {
                            M('attend_evection')->where('RUN_ID=' . $data['run_id'])->delete();
                        }
                        break;
                    case 145:       //加班
                        $countNum = M('attendance_overtime')->where('RUN_ID=' . $data['run_id'])->count();
                        if ($countNum > 0) {
                            M('attendance_overtime')->where('RUN_ID=' . $data['run_id'])->delete();
                        }
                        break;
                }
            }
        } else {
            $return['info'] = '回退失败';
        }
//        var_dump($resturn);
        $this->ajaxReturn($return);
    }

    //登陆日志
    public function log() {
        $map['is_mobile'] = array('neq', 0);
        $logs = M('user')->field('USER_ID,is_mobile,mobile_login_time')->where($map)->order('mobile_login_time desc')->select();
        $this->logs = $logs;
        $this->display();
    }

}
